(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:ReserveoptCtrl
     * @description
     * # ReserveoptCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('ReserveoptCtrl', ReserveoptCtrl);

    ReserveoptCtrl.$inject = ['$scope', 'dataService', 'helper', 'dhfprinterService', '$timeout', '$interval', '$rootScope', '$state', 'moment', '$location'];

    function ReserveoptCtrl($scope, dataService, helper, dhfprinterService, $timeout, $interval, $rootScope, $state, moment, $location) {
        $scope.listAction = function() {
            $scope.getlist(1);
        }

        $scope.getlist = function() {
            var query = {};
            if ($scope.reserve_opt_id) {
                query.query = {
                    reserve_opt_id: $scope.reserve_opt_id
                };
            }
            dataService.reserveoptGet(query).then(function(re1) {
                if (re1.reserveopts) {
                    $scope.reserve_opt = re1.reserveopts[0];
                }
            });
        }

        //添加数据 初始化
        $scope.addAction = function() {
            $scope.reserve_opt_id = $location.search().reserve_opt_id;
            if ($scope.reserve_opt_id) {
                $scope.getlist();
            } else {
                //初始化值
                $scope.reserve_opt = []
                $scope.reserve_opt.can_reserve_days = 7;
            }

        }

        // schema_type_id
        $scope.saveData = function() {
            var data = {};
            var zhuoziApiAry = {
                "add": "reserveoptAdd",
                "up": "reserveoptUpdate",
            }
            var api = "add";
            if ($scope.reserve_opt_id) {
                api = "up";
                data = $scope.reserve_opt
            }

            dataService[zhuoziApiAry[api]](data).then(function(r) {
                if (r.reserve_opt_id) {
                    helper.myAlert("success", null, null, function() {
                        $scope.stltodat();
                        $state.go("reserveopt.list");
                    });
                } else {
                    helper.myAlert("error");
                }
            })
        }

        //生成数据
        $scope.stltodat = function() {
            var query = {
                page_no: 1,
                page_size: 100,
                query: { store_id: $rootScope.store_id }
            };
            dataService.schemaGet(query).then(function(re1) {
                if (re1.schemas) {
                    var schemas = re1.schemas;
                    _.map(schemas, function(da, ke) {
                        if (da) {
                            var qu = { "schema_id": da.schema_id };
                            dataService.stltodataAdd(qu).then(function(re2) {
                                console.error("re2", re2);
                            });
                        }
                    });
                }
            });
        }
    }
})();